#include <QtGui>

#include "ListWidgetDemo.h"

ListWidgetDemo::ListWidgetDemo(const QMap<int, QString> &symbolMap,
							   QWidget *parent) : QDialog(parent)
{
	listWidget = new QListWidget;
	listWidget->setIconSize(QSize(60, 60));

	QListWidgetItem *item;
	QMapIterator<int, QString> it(symbolMap);
	while (it.hasNext()) {
		it.next();
		item = new QListWidgetItem(it.value(), listWidget);
		item->setIcon(iconForSymbol(it.value()));
		item->setData(Qt::UserRole, it.key());
	}

	buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
									 | QDialogButtonBox::Cancel);
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(listWidget);
	mainLayout->addWidget(buttonBox);
	setLayout(mainLayout);

	setWindowTitle(tr("List Widget Demo"));
	//setAttribute(Qt::WA_DeleteOnClose);
}

void ListWidgetDemo::done(int result)
{
	id = -1;
	if (result == QDialog::Accepted) {
		QListWidgetItem *item = listWidget->currentItem();
		if (item) {
			id = item->data(Qt::UserRole).toInt();
		}
	}
	QDialog::done(result);
}

QIcon ListWidgetDemo::iconForSymbol(const QString &symbol)
{
	QString fileName = "Resources/images/" + symbol.toLower();
	return QIcon(fileName);
}
